package spring.advancedweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import spring.advancedweb.excetion.UserNotFoundException;
import spring.advancedweb.excetion.DuplicateUserException;

import java.util.Map;

/**
 * Created by kassadin@foxmail.com on 2017/4/29 0029.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/{username}", method = RequestMethod.GET)
    public String getUser(@PathVariable String username, Map model) {
        if (StringUtils.isEmpty(username) || username.length() <= 3) {
            throw new UserNotFoundException();
        }
        model.put("username", username);
        return "user";
    }

    /**
     * 只处理正常情况，异常统一
     * /addUser?username=123
     * @param username
     * @param model
     * @return
     */
    @RequestMapping(value = "/addUser")
    public String saveUser(@RequestParam String username, Map model) {
        saveUserToDb(username);
        model.put("username", username);
        return "user";
    }

    public void saveUserToDb(String username) {
        throw new DuplicateUserException();
    }

//    @ExceptionHandler(DuplicateUserException.class)
//    public String handleDuplicateUser(DuplicateUserException duplicateUserException, Model model) {
//        System.out.println(duplicateUserException.getLocalizedMessage());
//        model.addAttribute("username", duplicateUserException.getLocalizedMessage());
//        return "user";
//    }

    ///user/redirectToShow?username=xxy&id=123
    @RequestMapping("/redirectToShow")
    public String redirectToShow(@RequestParam String username, @RequestParam long id, Model model) {
        System.out.println(username +":"+id);
        model.addAttribute("id",id);
        model.addAttribute("username",username);
        return "redirect:/user/show/{username}";
    }

    ///user/show/xxy?id=123
    @RequestMapping("/show/{username}")
    public String show(@PathVariable String username, @RequestParam long id, Model model) {
        model.addAttribute("username", username +":" +id);
        return "user";
    }

    @RequestMapping("/redirectToShowFlash")
    public String redirectToShowFlash(@RequestParam String username, @RequestParam long id, RedirectAttributes model) {
        System.out.println(username +":"+id);
        model.addFlashAttribute("id",id);
        model.addAttribute("username",username);
        return "redirect:/user/showFlash/{username}";
    }

    @RequestMapping("/showFlash/{username}")
    public String showFlash(@PathVariable String username, Model model) {
//        Object id = model.getFlashAttributes().get("id");
        Object id = model.asMap().get("id");
        model.addAttribute("username", username +":" +id);
        return "user";
    }

}
